package com.qianfeng.teachdemo.ui.webservice;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.http.RequestParams;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.http.internet.AjaxCallBack;
import com.qianfeng.teachdemo.http.internet.FastHttp;
import com.qianfeng.teachdemo.http.internet.InternetConfig;
import com.qianfeng.teachdemo.http.internet.ResponseEntity;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;

/**
 * Http
 * WebService 调用步骤  他是一种soap协议 返回的数据类型 xml
 * 第一步 指定 WebService 的命名空间和调用方法
 * 第二步 调用方法的参数,注意参数名可以不一样,但顺序必须一样
 * 第三步 生成调用Webservice方法的SOAP协议请求信息
 * 第四步 创建HttpTransportsSE对象。
 * 第五步 使用Call方法请求服务器
 * 第六步 获得webservice的请求结果
 */
public class WebServiceActivity extends ActionBarActivity {
    /**
     * 声明工作空间
     */
    private static final String NAMESPACE = "http://WebXml.com.cn/";

    /**
     * 声明方法访问
     */
    private static final String METHOD_NAME = "getWeatherbyCityName";
    /**
     * 声明访问地址
     */
    private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";

    private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";

    private TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_service);
        textView = (TextView) findViewById(R.id.wether);
        new Thread(new Runnable() {
            @Override
            public void run() {
            getWether();
            }
        }).start();

    }


    private void getWetherFromWebService() {
        InternetConfig config = InternetConfig.defaultConfig();
        config.setName_space(NAMESPACE);
        LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
        params.put("theCityName", "北京");
        FastHttp.ajaxWebServer(URL, METHOD_NAME, params, config, new AjaxCallBack() {
            @Override
            public boolean stop() {
                return false;
            }

            @Override
            public void callBack(ResponseEntity status) {
                String result = status.getContentAsString();
            }
        });
    }


    /**
     * 参数名字随便 但是顺序不能颠倒;
     *
     * @param theCityName
     * @param id
     */
    public void getWeatherbyCityName(String theCityName, String id) {

    }


    private void getWether() {
        //命名空间  访问的方法名
        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
        //添加参数
        soapObject.addProperty("theCityName", "北京");
        //SOAP协议
        //创建对象  并指定WebService服务器的版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        //
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;

        HttpTransportSE ht = new HttpTransportSE(URL);
        ht.debug = true;
        try {
            ht.call(SOAP_ACTION, envelope);
            //
            SoapObject resultObject = (SoapObject) envelope.getResponse();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

    }

    private String weatherToday;

    private void parseWeather(SoapObject detail) {
        String date = detail.getProperty(6).toString();
        weatherToday = "今天：" + date.split(" ")[0];
        weatherToday = weatherToday + "\n天气：" + date.split(" ")[1];
        weatherToday = weatherToday + "\n气温："
                + detail.getProperty(5).toString();
        weatherToday = weatherToday + "\n风力："
                + detail.getProperty(7).toString() + "\n";
        textView.setText(weatherToday);

    }


}
